package com.devplatform.service.impl.points;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devplatform.constants.DataVoCodeConstant;
import com.devplatform.mapper.points.PointsDetailMapper;
import com.devplatform.pojo.points.PointsDetail;
import com.devplatform.pojo.points.PointsReason;
import com.devplatform.pojo.vo.DataVo;
import com.devplatform.service.points.PointsCommonService;
import com.devplatform.service.points.PointsDetailService;
import com.devplatform.service.points.PointsReasonService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author 86156
 * @description 针对表【points_detail】的数据库操作Service实现
 * @createDate 2025-09-28 14:44:21
 */
@Service
public class PointsDetailServiceImpl extends ServiceImpl<PointsDetailMapper, PointsDetail>
        implements PointsDetailService {
    @Resource
    private PointsDetailMapper pointsDetailMapper;
    @Resource
    private PointsReasonService pointsReasonService;
    @Resource
    private PointsCommonService pointsCommonService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    //查看积分规则
    public DataVo ArticlePoints(Long userId, String reasonCode) {
        PointsReason reason = pointsReasonService.getByReasonCode(reasonCode);
        if (reason == null) {
            return DataVo.error(DataVoCodeConstant.INVALID_POINTS, "无效积分" + reasonCode);
        }
        if (reason.getReasonType() != 1) {
            return DataVo.error("该规则不支持积分发放：" + reasonCode);
        }
        if ("ARTICLE_LIKE".equals(reasonCode)) {
            int todayLikeCount = countTodayByReason(userId, reasonCode);
            if (todayLikeCount >= 10) {
                return DataVo.error("今日点赞积分已达上限（每日最多10次）");
            }
        }
        if ("ARTICLE_REPLY".equals(reasonCode)) {
            int todayReplyCount = countTodayByReason(userId, reasonCode);
            if (todayReplyCount >= 5) {
                return DataVo.error("今日回复评论积分已达上限（每日最多5次）");
            }
        }
        int pointsValue = reason.getPointsValue();
        //修改的积分
        pointsCommonService.updatePointsDetail(userId, reasonCode, pointsValue, true);
        return null;
    }


    private int countTodayByReason(Long userId, String reasonCode) {
        LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        return pointsDetailMapper.countByReasonToday(userId, reasonCode, start, end);
    }

    @Override
    public Page<PointsDetail> getDetail(Long userId, Integer pageNum, Integer pageSize, String reasonCode) {
        if (pageNum == null || pageNum < 1) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize < 1 || pageSize > 100) {
            pageSize = 10;
        }
        Page<PointsDetail> page = new Page<>(pageNum, pageSize);

        if (reasonCode != null && !reasonCode.isEmpty()) {
            return pointsDetailMapper.selectByUserIdAndReason(page, userId, reasonCode);
        } else {
            return pointsDetailMapper.selectByUserId(page, userId);
        }
    }


    @Override
    public PointsDetail getDetailById(Integer detailid, Long userId) throws IllegalAccessException {
        if (detailid == null || userId == null) {
            throw new IllegalAccessException("用户id与明细id不能为空");
        }
        return pointsDetailMapper.selectByDetailId(detailid, userId);
    }

    @Override
    //统计签到次数
    public int countToday(Long userId) {
        LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        return pointsDetailMapper.countSignInToday(userId, start, end);

    }
}




